//
//  DMFeedbackViewController.m
//  CQKuaiYi
//
//  Created by 何典钊 on 2017/1/10.
//  Copyright © 2017年 Mr.Cannon. All rights reserved.
//

#import "DMFeedbackViewController.h"
#import "DMFeedBackView.h"

@interface DMFeedbackViewController ()<UITextViewDelegate>

@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UILabel *placeHolderLabel;
@property (nonatomic, strong) UILabel *residueLabel;//输入了多少字

@end

@implementation DMFeedbackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupUI];
    
    UIBarButtonItem *barbutton = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStyleDone target:self action:@selector(PromptTheuser)];
    self.navigationItem.rightBarButtonItem = barbutton;
}

- (void)PromptTheuser {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"正在开发中" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void) setupUI {
    _textView = [[UITextView alloc] init];
    _textView.delegate = self;
    self.textView.layer.cornerRadius = 5.0;
    //    _textView.backgroundColor = [UIColor redColor];
    self.textView.layer.borderWidth = 1.0;//边宽
    self.textView.layer.cornerRadius = 20;//设置圆角
    self.textView.layer.borderColor =(__bridge CGColorRef _Nullable)([UIColor lightGrayColor]);
    _textView.font = [UIFont boldSystemFontOfSize:20];
    _textView.keyboardType = UIKeyboardTypeDefault;
    [self.view addSubview:_textView];
    
    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.offset(160);
    }];
    
    //    /再创建个可以放置默认字的lable
    self.placeHolderLabel = [[UILabel alloc]init];
    self.placeHolderLabel.numberOfLines = 0;
    self.placeHolderLabel.text = @"请输入你的意见最多140字";
    self.placeHolderLabel.backgroundColor =[UIColor clearColor];
    
    //  计算textview的输入字数
    self.residueLabel = [[UILabel alloc]initWithFrame:CGRectMake(360,140,60,20)];
    self.residueLabel.backgroundColor = [UIColor clearColor];
    self.residueLabel.font = [UIFont fontWithName:@"Arial" size:12.0f];
    self.residueLabel.text =[NSString stringWithFormat:@"140/140"];
    self.residueLabel.textColor = [[UIColor grayColor]colorWithAlphaComponent:0.5];
    [self.textView addSubview:_residueLabel];
    
    //添加
    [self.textView addSubview:self.placeHolderLabel];
    
    [_placeHolderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(20);
        make.left.offset(5);
    }];
    
    DMFeedBackView *feedBackView = [[DMFeedBackView alloc] initDMFeedBackView];
    [self.view addSubview:feedBackView];
    
    [feedBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_textView.mas_bottom);
        make.left.right.equalTo(self.view);
        make.height.offset(147);
    }];
}

- (void)textViewDidChange:(UITextView *)textView {
    
    if([textView.text length] == 0){
        
        self.placeHolderLabel.text = @"请输入你的意见最多140字";
    }else{
        
        self.placeHolderLabel.text = @"";//给空
    }
    //计算剩余字数
    NSString *nsTextCotent = textView.text;
    
    NSInteger scr = nsTextCotent.length;
    
    NSInteger remainTextNum = 140 - scr;
    
    self.residueLabel.text = [NSString stringWithFormat:@"%ld/140",remainTextNum];
}

@end

